<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
      layout:decorate="~{layout/base}">
<head>
    <title>订单详情</title>
    <link href="https://cdn.bootcdn.net/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <style>
        .product-image {
            width: 100px;
            height: 100px;
            object-fit: cover;
        }
        .timeline {
            position: relative;
            padding-left: 30px;
        }
        .timeline::before {
            content: '';
            position: absolute;
            left: 15px;
            top: 0;
            bottom: 0;
            width: 2px;
            background: #e9ecef;
        }
        .timeline-item {
            position: relative;
            padding-bottom: 1.5rem;
        }
        .timeline-item::before {
            content: '';
            position: absolute;
            left: -23px;
            top: 0;
            width: 12px;
            height: 12px;
            border-radius: 50%;
            background: #6c757d;
            border: 2px solid #fff;
        }
        .timeline-item.active::before {
            background: #28a745;
        }
    </style>
</head>
<body>
    <div layout:fragment="content">
        <div class="row mb-4">
            <div class="col">
                <nav aria-label="breadcrumb">
                    <ol class="breadcrumb">
                        <li class="breadcrumb-item"><a th:href="@{/orders}">我的订单</a></li>
                        <li class="breadcrumb-item active" th:text="${order.orderNo}">订单号</li>
                    </ol>
                </nav>
            </div>
        </div>

        <div class="row">
            <!-- 订单信息 -->
            <div class="col-md-8">
                <!-- 订单状态 -->
                <div class="card mb-4">
                    <div class="card-body">
                        <div class="d-flex align-items-center">
                            <div class="flex-grow-1">
                                <h5 class="mb-1">订单状态</h5>
                                <p class="mb-0 text-muted">
                                    <span th:class="'badge ' + 
                                        ${order.status == 0 ? 'bg-warning' : 
                                        (order.status == 1 ? 'bg-info' : 
                                        (order.status == 2 ? 'bg-success' : 'bg-secondary'))}"
                                        th:text="${order.status == 0 ? '待发货' : 
                                                (order.status == 1 ? '已发货' : 
                                                (order.status == 2 ? '已完成' : '已取消'))}">
                                        状态
                                    </span>
                                    <span class="ms-2" th:text="${order.statusDesc}">状态描述</span>
                                </p>
                            </div>
                            <div>
                                <button th:if="${order.status == 0}" 
                                        class="btn btn-outline-danger btn-sm"
                                        th:onclick="'cancelOrder(' + ${order.id} + ')'">
                                    取消订单
                                </button>
                                <button th:if="${order.status == 1}" 
                                        class="btn btn-success btn-sm"
                                        th:onclick="'confirmReceipt(' + ${order.id} + ')'">
                                    确认收货
                                </button>
                                <button th:if="${order.status == 2}" 
                                        class="btn btn-primary btn-sm"
                                        th:onclick="'window.location.href=\'/orders/' + ${order.id} + '/review\''">
                                    评价
                                </button>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- 商品信息 -->
                <div class="card mb-4">
                    <div class="card-header">
                        <h5 class="card-title mb-0">商品信息</h5>
                    </div>
                    <div class="card-body">
                        <div class="d-flex align-items-center mb-3" 
                             th:each="item : ${order.items}">
                            <img th:src="${item.productImage ?: '/static/images/default-product.jpg'}"
                                 class="product-image me-3" alt="商品图片">
                            <div class="flex-grow-1">
                                <h6 class="mb-1" th:text="${item.productName}">商品名称</h6>
                                <small class="text-muted" th:text="${item.specifications}">
                                    商品规格
                                </small>
                            </div>
                            <div class="text-end">
                                <div class="text-danger mb-1" 
                                     th:text="${'¥' + #numbers.formatDecimal(item.price, 1, 2)}">
                                    ¥0.00
                                </div>
                                <small class="text-muted" th:text="${'x' + item.quantity}">x1</small>
                            </div>
                        </div>
                        <hr>
                        <div class="row">
                            <div class="col text-end">
                                <div class="mb-2">
                                    <span class="text-muted">商品总额：</span>
                                    <span th:text="${'¥' + #numbers.formatDecimal(order.totalAmount, 1, 2)}">
                                        ¥0.00
                                    </span>
                                </div>
                                <div class="mb-2">
                                    <span class="text-muted">运费：</span>
                                    <span th:text="${'¥' + #numbers.formatDecimal(order.shippingFee, 1, 2)}">
                                        ¥0.00
                                    </span>
                                </div>
                                <div>
                                    <span class="text-muted">实付款：</span>
                                    <span class="text-danger fw-bold h5 mb-0" 
                                          th:text="${'¥' + #numbers.formatDecimal(order.totalAmount + order.shippingFee, 1, 2)}">
                                        ¥0.00
                                    </span>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- 收货信息 -->
                <div class="card mb-4">
                    <div class="card-header">
                        <h5 class="card-title mb-0">收货信息</h5>
                    </div>
                    <div class="card-body">
                        <div class="row">
                            <div class="col-md-3">
                                <div class="text-muted mb-2">收货人</div>
                                <div th:text="${order.receiverName}">收货人姓名</div>
                            </div>
                            <div class="col-md-3">
                                <div class="text-muted mb-2">联系电话</div>
                                <div th:text="${order.receiverPhone}">联系电话</div>
                            </div>
                            <div class="col-md-6">
                                <div class="text-muted mb-2">收货地址</div>
                                <div th:text="${order.receiverAddress}">收货地址</div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- 订单跟踪 -->
            <div class="col-md-4">
                <div class="card">
                    <div class="card-header">
                        <h5 class="card-title mb-0">订单跟踪</h5>
                    </div>
                    <div class="card-body">
                        <div class="timeline">
                            <div class="timeline-item" th:each="track : ${order.tracks}"
                                 th:classappend="${track.active ? 'active' : ''}">
                                <div class="text-muted mb-1" 
                                     th:text="${#temporals.format(track.time, 'yyyy-MM-dd HH:mm')}">
                                    时间
                                </div>
                                <div th:text="${track.description}">状态描述</div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script th:inline="javascript">
        function cancelOrder(orderId) {
            if (confirm('确定要取消这个订单吗？')) {
                fetch(`/orders/${orderId}/cancel`, {
                    method: 'POST'
                }).then(response => {
                    if (response.ok) {
                        location.reload();
                    }
                });
            }
        }

        function confirmReceipt(orderId) {
            if (confirm('确认已收到商品吗？')) {
                fetch(`/orders/${orderId}/confirm`, {
                    method: 'POST'
                }).then(response => {
                    if (response.ok) {
                        location.reload();
                    }
                });
            }
        }
    </script>
</body>
</html> 